import java.net.*;
import java.io.*;
import java.lang.Math;

public class Player implements Runnable
{
	private Socket socket;
	public boolean madeMove;
	public boolean canMove;
	public String name;
	public int atk, def, hp;
	public static final int ATK_MIN = 10, ATK_MAX = 15, DEF_MIN = 1, DEF_MAX = 5, HP_MIN = 100, HP_MAX = 125;

	public Player(Socket in)
	{
		socket = in;
		madeMove = false;
		canMove = true;
		name = "";

		atk = (int)(Math.random() * (ATK_MAX - ATK_MIN + 1) + ATK_MIN);
		def = (int)(Math.random() * (DEF_MAX - DEF_MIN + 1)  + DEF_MIN);
		hp = (int)(Math.random() * (HP_MAX - HP_MIN + 1) + HP_MIN);
	}

	public void run()
	{
		try{
			InputStreamReader isr = new InputStreamReader(socket.getInputStream());
			int c;
			while( (c = isr.read()) != -1)
			{
				//System.out.println("Setting madeMove =true");
				if(canMove)
					madeMove = true;
				else //Tried to play out of turn
				{
					sendMessage("You hit yourself.");
					this.hp -= this.atk - this.def;
				}
			}
		}
		catch(Exception e){}

	}

	public void sendMessage(String in) throws IOException
	{
		OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
		for(int x = 0; x < in.length(); x++)
			osw.write(in.charAt(x));

		osw.write(10);
		osw.flush();
	}

	public void loadName() throws IOException
	{
		InputStreamReader isr = new InputStreamReader(socket.getInputStream());
		int c;
		while( (c=isr.read()) != 10)
			name += (char)c;
	}

	public void start()
	{
		Thread makeThread = new Thread(this);
		makeThread.start();
	}

	public String stats()
	{
		String stats = "\n";
		stats += "Atk: " + atk;
		stats += "\nDef: " + def;
		stats += "\nHP: " + hp;
		stats += "\n";

		return stats;
	}
}